<template>
  <view class="container">
    <view class="content">
      <text class="title">数据库连接测试</text>
      <button @click="testConnection">测试数据库连接</button>
      <button @click="testUsersQuery">测试用户查询</button>
      <view class="result">
        <text>{{ result }}</text>
      </view>
    </view>
  </view>
</template>

<script>
const db = uniCloud.database()

export default {
  data() {
    return {
      result: '点击按钮开始测试'
    }
  },
  methods: {
    async testConnection() {
      try {
        this.result = '正在测试数据库连接...'
        const res = await db.get()
        this.result = '数据库连接成功: ' + JSON.stringify(res)
      } catch (err) {
        this.result = '数据库连接失败: ' + JSON.stringify(err)
        console.error('数据库连接失败:', err)
      }
    },
    
    async testUsersQuery() {
      try {
        this.result = '正在查询用户数据...'
        const res = await db.collection('uni-id-users').get()
        this.result = '用户查询成功: ' + JSON.stringify(res)
        console.log('用户数据:', res)
      } catch (err) {
        this.result = '用户查询失败: ' + JSON.stringify(err)
        console.error('用户查询失败:', err)
      }
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title {
  font-size: 24px;
  margin-bottom: 20px;
}

button {
  margin: 10px 0;
  padding: 10px 20px;
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 5px;
}

.result {
  margin-top: 20px;
  padding: 10px;
  background-color: #f0f0f0;
  border-radius: 5px;
  min-height: 100px;
  width: 100%;
  word-wrap: break-word;
}
</style>